package com.qf.controller.kucun_controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Commodity;
import com.qf.bean.Gnumber;
import com.qf.bean.Goods;
import com.qf.bean.Rstore;
import com.qf.service.kucun_service.GoodsService;
import com.qf.service.kucun_service.RstoreService;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
*
 *                                                 ___----~~~~~~~~~~~------___
 *                                   .  .   ~~//``....          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
*/
@Controller
@RequestMapping("/rstore")
public class RstoreController {
    @Resource
    private RstoreService rstoreService;

    @Resource
    private GoodsService goodsService;
    @RequestMapping("/all")
    public String query(Model model,Integer pageNum,String ruid,Integer rtype,Integer gnid,Integer state){
        if (pageNum==null || pageNum==0){
            pageNum=1;
        }
        PageHelper.startPage(pageNum, 3);
        List<Rstore> rstores = rstoreService.QueryRstore(ruid,rtype,gnid,state);
        PageInfo<Rstore> rstorePageInfo = new PageInfo<>(rstores);
        List<Gnumber> gnumbers = rstoreService.findnum();
        model.addAttribute("gnumbers",gnumbers);
        model.addAttribute("page",rstorePageInfo);
        model.addAttribute("store",rstores);
        return "/kucun/rstorelist";
    }
    @RequestMapping("/daochu")
    public void download(HttpServletResponse response, String[] ruids) throws IOException {
        List<Rstore> bgmExcelDownloads = rstoreService.findByruid(ruids);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("获取excel测试表格");
        HSSFRow row = null;
        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue("库存信息列表");//为第一行单元格设值

        /*为标题设计空间
         * firstRow从第1行开始
         * lastRow从第0行结束
         *从第1个单元格开始
         * 从第3个单元格结束
         */
        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
        sheet.addMergedRegion(rowRegion);

      /*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
      sheet.addMergedRegion(columnRegion);*/

        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("入库单号");//为第一个单元格设值
        row.createCell(1).setCellValue("源订单ID");//为第二个单元格设值
        row.createCell(2).setCellValue("创建日期");//为第三个单元格设值
        row.createCell(3).setCellValue("类型");//为第四个单元格设值
        row.createCell(4).setCellValue("供应商/客户名称");
        row.createCell(5).setCellValue("入库仓库");
        row.createCell(6).setCellValue("入库时间");
        row.createCell(7).setCellValue("经办人");
        row.createCell(8).setCellValue("状态");
        //遍历所获取的数据
        for (int i = 0; i < bgmExcelDownloads.size(); i++) {
            row = sheet.createRow(i + 2);
            Rstore rstore = bgmExcelDownloads.get(i);
            row.createCell(0).setCellValue(rstore.getRuid());
            row.createCell(1).setCellValue(rstore.getOid());
            row.createCell(2).setCellValue(rstore.getCtime());
            row.createCell(3).setCellValue(rstore.getRtype());
            row.createCell(4).setCellValue(rstore.getRname());
            row.createCell(5).setCellValue(rstore.getGnumber().getGname());
            row.createCell(6).setCellValue(rstore.getStoretime());
            row.createCell(7).setCellValue(rstore.getStoreadmin());
            row.createCell(8).setCellValue(rstore.getState());
        }
        sheet.setDefaultRowHeight((short) (16.5 * 20));
        //列宽自适应
        for (int i = 0; i <= 13; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=RuKu.xls");//默认Excel名称
        wb.write(os);
        os.flush();
        os.close();
    }

    @RequestMapping("/queryruid")
    public void queryruid(String ruid,HttpServletResponse response) throws Exception{
        Rstore ruid1 = rstoreService.findRuid(ruid);
        if (ruid1!=null){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }
    @RequestMapping("/addbefore")
    public String addbefore(Model model){
        List<Commodity> commodities = rstoreService.findcom();
        List<Gnumber> gnumbers = rstoreService.findnum();
        model.addAttribute("commodities",commodities);
        model.addAttribute("gnumbers",gnumbers);
        return "/kucun/rstoreadd";
    }

    @RequestMapping("/add")
    public String add(Rstore rstore,Integer number,Integer cid){
        try {
            Goods goods = goodsService.findcid(cid);
            Gnumber gnumber = goodsService.findgnid(rstore.getGnid());
            goodsService.updategoods(goods.getGid(), goods.getCnumber()+number);
            goodsService.updategnumber(gnumber.getGnid(), gnumber.getGnumber()+number);
        } catch (Exception e) {
            throw new RuntimeException("添加时修改失败");
        }
        try {
            rstore.setCtime(new Date());
            rstore.setStoretime(new Date());
            rstoreService.insert(rstore);
        } catch (Exception e) {
            throw new RuntimeException("添加失败");
        }
        return "redirect:/rstore/all";
    }

    @RequestMapping("/updatebefore")
    public String updatebefore(String ruid,Model model){
        Rstore rstore = rstoreService.findRuid(ruid);
        List<Gnumber> gnumbers = rstoreService.findnum();
        model.addAttribute("gnumbers",gnumbers);
        model.addAttribute("rstore",rstore);
        return "/kucun/update";
    }

    @RequestMapping("/update")
    public String update(Rstore rstore){
        try {
            rstoreService.update(rstore);
        } catch (Exception e) {
            throw new RuntimeException("修改失败");
        }
        return "redirect:/rstore/all";
    }

    @RequestMapping("/detail")
    public String detail(String ruid,Model model){
        Rstore rstore = rstoreService.findRuid(ruid);
        model.addAttribute("rstore",rstore);
        return "/kucun/rstoredetail";
    }

    /*@RequestMapping("/findnum")
    public String findallnum(Model model){
        List<Gnumber> gnumbers = rstoreService.findnum();
        model.addAttribute("gnumbers",gnumbers);
        return "/kucun/rstoredetail";
    }*/
}
